﻿using CodeCube.Common;
using System;

namespace CodeMaker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("模型生成工具");
            var dbc = new CodeCube.Core.DbContext("server=10.12.17.9;database=codecube;uid=open;pwd=123456;charset='utf8';SslMode=None");
            var ModelPath = FileHelperCore.MapPath("../../../../CodeCube.Service/Model");
            var ImplementsPath = FileHelperCore.MapPath("../../../../CodeCube.Service/Implements/");
            var InterfacesPath = FileHelperCore.MapPath("../../../../CodeCube.Service/Interfaces/");
            #region 删除旧文件
            //try
            //{
            //    var oldmodelfiles = FileHelper.GetFileNames(ModelPath);
            //    foreach (var delfile in oldmodelfiles)
            //    {
            //        FileHelper.DeleteFile(delfile);
            //    }
            //    var oldimplementfiles = FileHelper.GetFileNames(ImplementsPath);
            //    foreach (var delfile in oldimplementfiles)
            //    {
            //        FileHelper.DeleteFile(delfile);
            //    }
            //    var oldinterfacefiles = FileHelper.GetFileNames(InterfacesPath);
            //    foreach (var delfile in oldinterfacefiles)
            //    {
            //        FileHelper.DeleteFile(delfile);
            //    }
            //}catch(Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
            #endregion
            try
            {
                var tblist = dbc.Db.DbMaintenance.GetTableInfoList();
                var ImplementTemplate = FileHelper.FileToString(FileHelperCore.MapPath("ImplementTemplate.txt"));
                var InterfaceTemplate = FileHelper.FileToString(FileHelperCore.MapPath("InterfaceTemplate.txt"));
                foreach (var t in tblist)
                {
                    var newNames = t.Name.Split('_');
                    string newName = "";
                    foreach (var n in newNames)
                    {
                        newName += n.Substring(0, 1).ToUpper() + n.Substring(1);
                    }
                    Console.WriteLine(newName);
                    dbc.Db.MappingTables.Add(newName + "Model", t.Name);
                    Console.WriteLine("生成" + newName + "接口");
                    FileHelper.CreateFile(InterfacesPath + "I" + newName + "Service.cs", System.Text.Encoding.UTF8.GetBytes(InterfaceTemplate.Replace("$ClassName$", newName + "Service").Replace("$ModelName$", newName + "Model")));
                    Console.WriteLine("生成" + newName + "工具");
                    FileHelper.CreateFile(ImplementsPath + newName + "Service.cs", System.Text.Encoding.UTF8.GetBytes(ImplementTemplate.Replace("$ClassName$", newName + "Service").Replace("$ModelName$", newName + "Model")));
                }
                var vlist = dbc.Db.DbMaintenance.GetViewInfoList();
                foreach(var t in vlist)
                {
                    var newNames = t.Name.Split('_');
                    string newName = "";
                    foreach (var n in newNames)
                    {
                        newName += n.Substring(0, 1).ToUpper() + n.Substring(1);
                    }
                    Console.WriteLine(newName);
                    dbc.Db.MappingTables.Add(newName + "Model", t.Name);
                    Console.WriteLine("生成" + newName + "接口");
                    FileHelper.CreateFile(InterfacesPath + "I" + newName + "Service.cs", System.Text.Encoding.UTF8.GetBytes(InterfaceTemplate.Replace("$ClassName$", newName + "Service").Replace("$ModelName$", newName + "Model").Replace("Base","BaseV")));
                    Console.WriteLine("生成" + newName + "工具");
                    FileHelper.CreateFile(ImplementsPath + newName + "Service.cs", System.Text.Encoding.UTF8.GetBytes(ImplementTemplate.Replace("$ClassName$", newName + "Service").Replace("$ModelName$", newName + "Model").Replace("Base", "BaseV")));
                }
                Console.WriteLine("生成数据模型");
                dbc.Db.DbFirst
                    .IsCreateAttribute().CreateClassFile(ModelPath, "CodeCube.Service.Model");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
